use std::{env, fs, path::PathBuf};

fn main() {
    // 获取目标目录路径
    let proj_dir = env::var("CARGO_MANIFEST_DIR").expect("target not found");
    let proj_path = PathBuf::from(proj_dir);

    // 获取目标目录路径（target/debug 或 target/release）
    let profile = env::var("PROFILE").expect("PROFILE not set");
    let dst_path = proj_path.join("target").join(profile);

    // 源资源文件路径
    let src_path = proj_path.join("resources");

    // 如果目标文件夹存在resources文件夹，则删除
    let tgr_path = dst_path.join("resources");

    if tgr_path.exists() {
        fs::remove_dir_all(&tgr_path).expect("remove resources failed");
    }

    // 递归复制目录内容
    copy_dir_recursively(&src_path, &tgr_path).expect("copy resources failed");
}

fn copy_dir_recursively(src: &PathBuf, dst: &PathBuf) -> std::io::Result<()> {
    println!("copy {} to {}", src.display(), dst.display());
    if src.is_dir() {
        fs::create_dir_all(dst)?;
        for entry in fs::read_dir(src)? {
            let entry = entry?;
            let file_name = entry.file_name();
            copy_dir_recursively(&entry.path(), &dst.join(file_name))?;
        }
    } else {
        fs::copy(src, dst)?;
    }
    Ok(())
}
